#!/bin/bash
# USAGE:
#  mvDorisFiles sourceFolder destinationFolder
#  mvDorisFiles -s "/exact/path/to/source/Folder" destinationFolder
# 
# DESCRIPTION:
#  This script modifies Doris Result Files (*.res) and Doris Input Files
#  (*.drs) by changing locations of the output files to their current locations. 
#  It is a mere string replacement utility. It does not check for errors.
#  Each modified file is backed up with a .bck suffix (i.e *.res.bck) 
# 
#  -s: option is used to update *.res files that are already moved to their
#      destination, i.e. scp'ed from a different computer. With this option
#      mvDorisFiles only tries to update files without actually moving anything. 
# 
# EXAMPLES:
#  mvDorisFiles -s '/data/batu/adore_MexicoCity2/process/3DKalman/i12s' '/home/bosmanoglu/des3/kalman/3D/data'
#


#tmpFile="mvDoris.temporary.file"
#rm -rf ${tmpFile}

if [ $# -lt 2 ]; then
  error "Not enough input parameters."
  error "Please check usage with: ? mvDorisFiles"
  return 
fi

if [[ ${1} == "-s" ]];then
	sourceFolder=${2}
	destinationFolder=${3}
else
	sourceFolder=${1}
	destinationFolder=${2}
	echo "Moving Files"
	mv ${sourceFolder} ${destinationFolder}
fi
echo "Source Folder: ${sourceFolder}"
echo "Destination F: ${destinationFolder}"

#find ${destinationFolder} -name *.{res,drs,md5} > ${tmpFile}
#find ${destinationFolder} -name *.drs >> temporary.file
#find ${destinationFolder} -name *.md5 >> temporary.file
#for file in `ls ${destinationFolder}/*{res,drs}`
#for file in `find ${destinationFolder} \( -name "*.res" -o -name "*.drs" -o -name "*.set" \) `
#do
##	sed "s@${sourceFolder}@${destinationFolder}@g" ${file} > ${file}.new
##	mv ${file} ${file}.bck
##	mv ${file}.new ${file}
#        #echo "Correcting: ${file}"
#	sed --in-place='' "s@${sourceFolder}@${destinationFolder}@g" ${file}
#done	
find ${destinationFolder} \( -name "*.res" -o -name "*.drs" -o -name "*.set" \) -exec sed -i "s@${sourceFolder}@${destinationFolder}@g" {} \;

#rm -rf ${tmpFile}
echo "All done"
#exit 0	